package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;

import view.dialog.TransactionForm;
import controller.Controller;

@SuppressWarnings("serial")
public class View extends JPanel implements Observer {
	private Controller controller;
	private CardLayout cardLayout;
	private Component yearView;
	private QuickView quickView;
	private JPanel centerView;
	private JButton btnChangeView;
	private TransactionForm transactionForm;
	private Object parent;

	/**
	 * @wbp.parser.constructor
	 */
	public View(DailySpendingsWindow dailySpendingsWindow) {
		parent = dailySpendingsWindow;
		genericConstructor(dailySpendingsWindow);
	}

	public View(DailySpendingsApplet dailySpendingsApplet) {
		parent = dailySpendingsApplet;
		genericConstructor(dailySpendingsApplet);
	}

	private void genericConstructor(Container container) {
		this.controller = new Controller(this);
		
		setLayout(new BorderLayout(0, 0));

		centerView = new JPanel();
		add(centerView, BorderLayout.CENTER);
		cardLayout = new CardLayout(0, 0);
		centerView.setLayout(cardLayout);

		quickView = new QuickView(controller, this);
		centerView.add(quickView, "monthview");

		yearView = new TableView(controller);
		centerView.add(yearView, "yearview");

		JMenuBar menuBar = new JMenuBar();
		add(menuBar, BorderLayout.NORTH);

		JMenu mnFile = new JMenu("Datei");
		menuBar.add(mnFile);

		JMenuItem mntmOpen = new JMenuItem("\u00D6ffnen");
		mntmOpen.setActionCommand("openfile");
		mntmOpen.addActionListener(controller);
		mnFile.add(mntmOpen);
		
		JMenuItem mntmSichern = new JMenuItem("Sichern");
		mntmSichern.setActionCommand("save#now");
		mntmSichern.addActionListener(controller);
		mnFile.add(mntmSichern);
		
		JMenuItem mntmSpeichernUnter = new JMenuItem("Speichern unter");
		mntmSpeichernUnter.setActionCommand("save#as");
		mntmSpeichernUnter.addActionListener(controller);
		mnFile.add(mntmSpeichernUnter);

		JMenuItem mntmConnect = new JMenuItem("Connect");
		mnFile.add(mntmConnect);

		JMenu mnStyle = new JMenu("Style");
		menuBar.add(mnStyle);

		ButtonGroup styleGroup = new ButtonGroup();
		for (LookAndFeelInfo lookAndFeelInfo : UIManager
				.getInstalledLookAndFeels()) {
			JRadioButtonMenuItem lookAndFeelSelector = new JRadioButtonMenuItem(
					lookAndFeelInfo.getName());
			lookAndFeelSelector.setActionCommand("style#"
					+ lookAndFeelInfo.getClassName());
			lookAndFeelSelector.addActionListener(controller);
			styleGroup.add(lookAndFeelSelector);
			mnStyle.add(lookAndFeelSelector);

			if (lookAndFeelInfo.getName().equals(
					UIManager.getLookAndFeel().getName())) {
				lookAndFeelSelector.setSelected(true);
			}
		}

		JPanel westPanel = new JPanel();
		add(westPanel, BorderLayout.WEST);
		westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS));

		btnChangeView = new JButton("Details");
		btnChangeView.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnChangeView.setActionCommand("switchto#yearview");
		btnChangeView.addActionListener(controller);
		westPanel.add(btnChangeView);

		JPanel transactionFormPanel = new JPanel();
		transactionFormPanel.setBorder(new TitledBorder(null, "neue Transaktion",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		westPanel.add(transactionFormPanel);
		transactionFormPanel.setLayout(new BorderLayout(0, 0));
		transactionForm = new TransactionForm(controller);
		transactionFormPanel.add(transactionForm, BorderLayout.CENTER);

		
		JScrollPane eastPanel = new JScrollPane(new TransactionPanel(controller));
		eastPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		eastPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		eastPanel.setBorder(new TitledBorder(null, "Transactions",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(eastPanel, BorderLayout.EAST);
		
//		switchTo("yearview"); // TODO for quick debug
	}

	public JButton getBtnChangeView() {
		return btnChangeView;
	}
	
	public void switchTo(String card) {
		cardLayout.show(centerView, card);
	}

	@Override
	public void update(Observable arg0, Object arg1) {

	}
	
	public TransactionForm getTransactionForm() {
		return transactionForm;
	}
	
	public Object getParentContainer() {
		return parent;
	}
}
